<?php
namespace tool;
use think\facade\Db;
class Utils
{
    /*
     * 转换为树形结构
     * */
    public static function tree($array , $fu='fid',$id='id',$level=0){
        //第一步 构造数据
        $items = array();
        foreach($array as $value){
            $items[$value[$id]] = $value;
        }
        //第二部 遍历数据 生成树状结构
        $tree = array();
        foreach($items as $key => $item){
            if(isset($items[$item[$fu]])){
                $items[$item[$fu]]['children'][] = &$items[$key];
            }else{
                $tree[] = &$items[$key];
            }
        }
        return $tree;
    }
    /*
     * 上下级别列表
     * */
    public static function upperLowerLevelList ($array , $fu='fid',$id='id',$level=0){
        $newArray = [];
        foreach ($array as $item) {
            if ($item[$fu] == 0) {
                $item['level']=$level;
                $newArray[] = $item;
                foreach ($array as $subItem) {
                    if ($subItem[$fu] == $item[$id]) {
                        $subItem['level']=$level+1;
                        $newArray[] = $subItem;
                    }
                }
            }
        }
      //  dump($newArray,5555);
        return $newArray;
    }
}